| Conditions | 1 |
| Paths | 4 |
| Total Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var store=''; |
||
| 86 | function repeat() |
||
| 87 | { |
||
| 88 | // console.log(element); |
||
| 89 | init(1); |
||
| 90 | |||
| 91 | var q={"username":element.id,"load":num}; |
||
| 92 | q="q="+JSON.stringify(q); |
||
| 93 | |||
| 94 | // console.log(q); |
||
| 95 | |||
| 96 | var xmlhttp = new XMLHttpRequest(); |
||
| 97 | var ele=document.getElementById("conversation"); // ajax call |
||
| 98 | xmlhttp.onreadystatechange = function() |
||
| 99 | { |
||
| 100 | if (xmlhttp.readyState == 4 && xmlhttp.status == 200) |
||
| 101 | { |
||
| 102 | var arr=xmlhttp.responseText; |
||
| 103 | arr=JSON.parse(arr); |
||
| 104 | // console.log(arr); |
||
| 105 | |||
| 106 | if(arr!=null && flag==0) |
||
| 107 | { |
||
| 108 | // console.log(1); |
||
| 109 | if (arr[arr.length-1]==1) // Old User |
||
| 110 | { |
||
| 111 | var a=arr[0].id; |
||
| 112 | if(store!=a) |
||
| 113 | { |
||
| 114 | store=a; |
||
| 115 | // console.log(1); |
||
| 116 | ele.innerHTML=""; |
||
| 117 | |||
| 118 | if(arr[arr.length-2].load>10) // For showing previous message |
||
| 119 | { |
||
| 120 | var txt=$("<a></a>").text("Show Previous Message!"); |
||
| 121 | var te=$("<div></div>").append(txt); |
||
| 122 | $("#conversation").append(te); |
||
| 123 | $("#conversation div").addClass("previous"); |
||
| 124 | $("#conversation div a").attr({"onclick":"previous(this)","id":arr[0].username,"name":arr[arr.length-2].load}); |
||
| 125 | } |
||
| 126 | |||
| 127 | for (var i = arr.length -3; i >= 0; i--) |
||
| 128 | { |
||
| 129 | // create element |
||
| 130 | var para=document.createElement("div"); |
||
| 131 | |||
| 132 | if(arr[i]['sent_by']!=arr[i]['start']) |
||
| 133 | para.setAttribute('class','sender'); |
||
| 134 | else |
||
| 135 | para.setAttribute('class','receiver'); |
||
| 136 | |||
| 137 | ele.appendChild(para); |
||
| 138 | var bre=document.createElement("br"); |
||
| 139 | bre.setAttribute("style","clear:both;"); |
||
| 140 | ele.appendChild(bre); |
||
| 141 | |||
| 142 | var info=document.createElement("p"); |
||
| 143 | var node=document.createTextNode(arr[i]['message']); |
||
| 144 | info.appendChild(node); |
||
| 145 | para.appendChild(info); |
||
| 146 | |||
| 147 | var tt=document.createElement("h6"); |
||
| 148 | var inp=document.createTextNode(arr[i]['time']); |
||
| 149 | tt.appendChild(inp); |
||
| 150 | tt.setAttribute('class','message_time'); |
||
| 151 | info.appendChild(tt); |
||
| 152 | } |
||
| 153 | |||
| 154 | $("#chat_heading a").remove('a'); |
||
| 155 | var txt=$("<a></a>").text(arr[0].name); |
||
| 156 | $("#chat_heading").append(txt); |
||
| 157 | $("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr[0].username}); |
||
| 158 | $("#text_reply").attr({'name':arr[0]['identifier_message_number']}); |
||
| 159 | ele.scrollTop = ele.scrollHeight; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | else if(arr['new']==0) // New User |
||
| 163 | { |
||
| 164 | ele.innerHTML=""; |
||
| 165 | $("#chat_heading a").remove('a'); |
||
| 166 | var txt=$("<a></a>").text(arr.name); |
||
| 167 | $("#chat_heading").append(txt); |
||
| 168 | $("#chat_heading a").attr({"href":"http://localhost/openchat/account.php/"+arr.username}); |
||
| 169 | $("#text_reply").attr({'name':arr['identifier_message_number']}); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | }; |
||
| 174 | xmlhttp.open("POST", "ajax/chat.php", true); |
||
| 175 | xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
||
| 176 | xmlhttp.send(q); |
||
| 177 | } |
||
| 178 | function stop() |
||
| 365 | console.log("Hello, Contact me at [email protected]"); |
||
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.